$(function(){

    /**
     * =================================================================
     * 预约看房
     * @Author   King
     * @DateTime 2017-07-21T15:39:05+0800
     * =================================================================
     */
    var appoint = function(fu_mobile,e){
        $("#appointModal").modal("hide");
        //关闭弹窗
        var data = {
            apartment_id:$("body").attr("data-id"),
            fu_mobile:fu_mobile,
            apt_type:1
        };

        $.post(base.site_url('client/memMng/memAppointment/addAppointment'),data, function(ret) {
            if(ret.code === 3 || ret.code == 4 || ret.code == 5){
                if($("#submit_appointment").length > 0){
                    _hmt.push(['_trackEvent', 'pc', 'appointment', 'zufang']);
//					recordAppointBtnClicked(true);
                }
                var apptSucessPopup = dialog({
                    title:'预约成功',
                    width:500,
                    height:300,
                    padding:0,
                    cancel:function(){
                        window.location.reload();
                        return false;
                    },
                    cancelDisplay:false
                })
                    .showModal();

                //获取二维码
                var data = {
                    mem_appointment_id:ret.data
                };

                $.post(base.site_url('client/apart/apart/createUserApptQrcode'),data,function(ret){
                    if(ret.code === 0){
                        apptSucessPopup.content('<div class="t-c wx-wrap">'+
                            '<div><img src="'+ret.data+'" width="144" height="144" /></div>'+
                            '<div class="wx-info">'+
                            '<p>打开微信扫一扫，关注“租房网”微信公众号</p>'+
                            '<p>实时接收预约通知，查看联系方式，一键导航</span></p>'+
                            '</div>'+
                            '</div>');
                    }
                },'json');
            }else{
                base.alertMsg({
                    text:ret.message
                });
            }
        }, 'json');
    };

    /**
     * =================================================================
     * 收藏
     * @Author   King
     * @DateTime 2017-07-21T15:39:05+0800
     * =================================================================
     */
    $('[data-toggle="user:collect"]').click(function(){
        var user = new User();
        var href=$(this).data('href');
        if (user.isLogin()) {
            var $this = $(this),
                text = $this.text(),
                data = {
                    type:'',
                    id:$("body").attr("data-id")
                };
            if(href=='zufang'){
                data.type='zufang';
            }else if(href=='office'){
                data.type='office';
            }

            if(text == "已收藏"){
                $.post(base.site_url('client/memMng/memCollection/deleteMemCollection'),data, function(ret) {
                    if (ret.code === 0) {
                        $this.text('收藏');
                    }else{
                        alert(ret.message);
                    }
                }, 'json');
            }else{
                $.post(base.site_url('client/memMng/memCollection/collect'),{data: JSON.stringify(data)}, function(ret) {
                    if (ret.code === 0) {
                        $this.text('已收藏');
                    }else{
                        alert(ret.message);
                    }
                }, 'json');
            }
        }else{
            $("#loginModal").modal("show");
            User.prototype.callback = function(){
                $('[data-toggle="user:collect"]').first().click();
            }
        }

    });

    /**
     * =================================================================
     * 统计预约
     * @Author   King
     * @DateTime 2017-07-21T15:39:05+0800
     * =================================================================
     */
    var recordAppointBtnClicked = function(submitBaidu){
        var src_url = '';
        try{
            src_url = base.source_page()
        }catch(ex){
            console.log(ex);
        }

        var data = {
            url:window.location.href,
            src_url:src_url,
            fu_mobile:$("#appointForm").find("input[name='fu_mobile']").val(),
            btn_type:3,
            ra_submit_baidu:submitBaidu || false
        };
        $.post(base.site_url('client/apart/apart/recordAppointBtnClicked'),{data: JSON.stringify(data)},function(ret){});
    };
});